package com.example.lin.config;

import com.example.lin.component.CustomerUserDetailsService;
import com.example.lin.config.handler.AnonymousAuthenticationHandler;
import com.example.lin.config.handler.CustomerAccessDeniedHandler;
import com.example.lin.config.handler.LoginFailureHandler;
import com.example.lin.config.handler.LoginSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    protected LoginSuccessHandler loginSuccessHandler;

    @Resource
    protected LoginFailureHandler loginFailureHandler;

    @Resource
    protected AnonymousAuthenticationHandler anonymousAuthenticationHandler;

    @Resource
    protected CustomerAccessDeniedHandler customerAccessDeniedHandler;

    @Resource
    protected CustomerUserDetailsService customerUserDetailsService;

    /**
     * 注入加密类
     * @return
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 处理登录认证
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//      登录处理过程
        http.formLogin()
                .successHandler(loginSuccessHandler)    //认证成功处理器
                .failureHandler(loginFailureHandler)    //认证失败处理器
                .and()
                .csrf()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()    //设置需要拦截的请求
                .antMatchers("/login").permitAll()  //登录请求放行(不拦截)
                .anyRequest().authenticated()   //其它请求一律都需要进行身份验证
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(anonymousAuthenticationHandler)   //匿名无权限访问
                .accessDeniedHandler(customerAccessDeniedHandler)   //认证用户无权限访问
                .and()
                .cors();    //支持跨域请求
    }

    /**
     * 配置认证处理器
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customerUserDetailsService).passwordEncoder(passwordEncoder());
    }
}
